﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Z13qu.Code;
using Z13qu.Model;
using Z13qu.Service;
using Z13qu.Web.Attributes;

namespace Z13qu.Web.Controllers
{
    [Login]
    public class ClientsDataController : Controller
    {
        public JsonResult GetClientsDataJson()
        {
            var data = new
            {
                //dataItems = this.GetDataItemList()
                //organize = this.GetOrganizeList(),
                //role = this.GetRoleList(),
                //duty = this.GetDutyList(),
                //user = "",
                authorizeMenu = this.GetMenuList(),
                //authorizeButton = this.GetMenuButtonList(),
            };
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        private object GetDataItemList()
        {
            return null;
            //var itemdata = new ItemsDetailApp().GetList();
            //Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            //foreach (var item in new ItemsApp().GetList())
            //{
            //    var dataItemList = itemdata.FindAll(t => t.F_ItemId.Equals(item.F_Id));
            //    Dictionary<string, string> dictionaryItemList = new Dictionary<string, string>();
            //    foreach (var itemList in dataItemList)
            //    {
            //        dictionaryItemList.Add(itemList.F_ItemCode, itemList.F_ItemName);
            //    }
            //    dictionaryItem.Add(item.F_EnCode, dictionaryItemList);
            //}
            //return dictionaryItem;
        }
        private List<Sys_ModuleRec> GetMenuList()
        {
            var roleId = OperatorProvider.Provider.GetCurrent().RoleId;
            List<Sys_Module> modules = new Sys_ModuleService().GetMenuList(roleId);

            List<Sys_ModuleRec> menuInfos = new List<Sys_ModuleRec>();
            List<Sys_Module> topModules = modules.FindAll(t => t.ParentId == "0");
            topModules.ForEach(item =>
            {
                var moduleRec = ConvertToSys_ModuleRec(item);
                menuInfos.Add(moduleRec);
                ToMenuJson(moduleRec, modules, item.Id);
            });
            //return ToMenuJson(result, modules, "0");
            return menuInfos;
        }
        private void ToMenuJson(Sys_ModuleRec moduleRec, List<Sys_Module> data, string parentId)
        {
            List<Sys_Module> entitys = data.FindAll(t => t.ParentId == parentId);
            if (entitys.Count > 0)
            {
                moduleRec.ChildNodes = new List<Sys_ModuleRec>();
                entitys.ForEach(item=> {
                    var tmpModuleRec = ConvertToSys_ModuleRec(item);
                    moduleRec.ChildNodes.Add(tmpModuleRec);
                    ToMenuJson(tmpModuleRec, data, item.Id);
                });
            }
        }

        private Sys_ModuleRec ConvertToSys_ModuleRec(Sys_Module module)
        {
            return new Sys_ModuleRec()
            {
                Id = module.Id,
                ParentId = module.ParentId,
                Name = module.Name,
                Icon = module.Icon,
                UrlAddress = module.UrlAddress,
                IsMenu = module.IsMenu,
                IsExpand = module.IsExpand,
                AllowEdit = module.AllowEdit,
                AllowDelete = module.AllowDelete,
                SortCode = module.SortCode,
                IsDelete = module.IsDelete,
                IsEnable = module.IsEnable,
                Description = module.Description,
                CreatorTime = module.CreatorTime,
                CreatorUserId = module.CreatorUserId,
                LastModifyTime = module.LastModifyTime,
                LastModifyUserId = module.LastModifyUserId,
                DeleteTime = module.DeleteTime,
                DeleteUserId = module.DeleteUserId
            };
        }
    }
}
